import React, { Component } from 'react'
import HouseItem from '../../components/HouseItem'
import { BASE_URL } from '../../utils'
import NavHeader from '../../components/NavHeader'
import NoHouse from '../../components/NoHouse'
export default class index extends Component {
  state = {
    list: []
  }
  async getData() {
    let res = await this.$API.get('/user/favorites')
    const { status, body } = res
    if (status === 200) {
      this.setState({
        list: body
      })
    } else {
      const { history, location } = this.props
      history.replace('/login', {
        from: location
      })
    }
  }
  componentDidMount() {
    this.getData()
  }
  render() {
    let { list } = this.state
    return (
      <div className="collection">
        <NavHeader>我的收藏</NavHeader>
        {
          list.length===0?<NoHouse>暂无收藏房源</NoHouse>:
          list.map(item =>
            <HouseItem
              key={item.houseCode}
              onClick={() => this.props.history.push(`/detail/${item.houseCode}`)}
              src={BASE_URL + item.houseImg}
              title={item.title}
              desc={item.desc}
              tags={item.tags}
              price={item.price}
            />)
        }
      </div>
    )
  }
}
